<template>
  <div id="type">
    <el-card shadow="always">
      <el-form :inline="true" size="medium" :model="typeSearch" class="demo-form-inline">
        <!-- 查询表单 -->
        <el-form-item>
          <el-input v-model="typeSearch.tname" placeholder="类型名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="el-icon-plus" plain @click="addType">添加类型</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" icon="el-icon-remove" plain @click="handleDelete">批量删除</el-button>
        </el-form-item>
      </el-form>
      <!-- 类型列表表格 -->
      <el-table
          :data="typeList"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection">
        </el-table-column>
        <el-table-column
            label="类型id"
            align="center"
            prop="tid"
            width="180"/>
        <el-table-column
            label="类型名"
            prop="tname"
            align="center"/>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页控件 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>

    <!-- 编辑/新增类型对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="type" :rules="rules" ref="ruleForm">
        <el-form-item label="类型名" prop="tname" :label-width="formLabelWidth">
          <el-input v-model="type.tname" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveType">保 存</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import typeApi from "@/api/type"

export default {
  name: "TypeView",
  data() {
    return {
      typeList: [],
      page: 1,        // 当前面
      pageSize: 5,    // 每页显示记录条数
      total: 100,     // 总记录数
      typeSearch: {}, // 查询条件对象
      dialogFormVisible: false,  // 控制对话框显示
      type: {},       // 当前操作的类型对象
      title: '添加类型',  // 对话框标题
      formLabelWidth: '100px',  // 代表表单的标签宽度
      multipleSelection:[],     // 选中的多项类型
      tids: [],                 // 删除的类型编号列表

    }
  }
  ,
  created() {
    this.search()
  },
  methods: {

    // 条件查询带分页，""可查出全部数据
    async search() {
      const {data: res} = await typeApi.search(this.page, this.pageSize, this.typeSearch)
      if (res.code === 200) {
        this.typeList = res.data.pr.rows
        this.total = res.data.pr.total
      }
    },
    //添加类型
    addType() {
      //跟修改框共用得改标题。
      this.title = "添加类型"


      this.type = {}
      this.dialogFormVisible = true
    },
    // 保存类型
    saveType() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          const url = this.type.tid ? '/type/update' : '/type/add';
          const method = this.type.tid ? 'put' : 'post';
          const {data:res} = await typeApi.save(url,method,this.type)
          console.log("res:",res)
          if (res.code === 200) {
            this.$message({ type: 'success', message: res.message });
            this.dialogFormVisible = false;
            this.search();
          } else {
            this.$message({ type: 'error', message: res.message });
          }

        } else {
          console.log("验证失败！")
          return false
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection)
    },
    // 当pagesize大小发生改变时触发
    handleSizeChange(val) {
      this.pageSize = val
      this.search()
      console.log(`每页 ${val} 条`);
    },
    // 当页码page发生改变时触发
    handleCurrentChange(val) {
      this.page = val
      this.search()
      console.log(`当前页: ${val}`);
    },
    // 修改类型
    handleEdit(index, row) {

      this.title = '修改类型'

      //修改需要信息回显
      this.type = row
      this.dialogFormVisible = true
    },
    handleDelete(index, row) {
      this.tids = row ? [row.tid] : this.multipleSelection.map(m => m.tid);
      this.$confirm(`确认删除编号为${this.tids}的类型吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let _this = this
        typeApi.delete(this.tids)
            .then(({data:res}) => {
              console.log("res:",res)
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: res.message,
                  onClose: () => {
                    _this.search()
                  }
                })
              } else {
                this.$message({
                  type: 'error',
                  message: res.message
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style>

.el-card {
  width: 100%;
}

.el-pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
